package com.example.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.core.constants.Constants;
import com.example.common.core.enums.ResultCode;
import com.example.common.core.utils.ThreadLocalUtil;
import com.example.common.security.exception.ServiceException;
import com.example.system.domain.chat.Group;
import com.example.system.domain.chat.GroupMember;
import com.example.system.domain.classroom.Classroom;
import com.example.system.domain.classroom.dto.ClassroomQueryDTO;
import com.example.system.domain.classroom.dto.ClassroomSaveDTO;
import com.example.system.domain.classroom.vo.ClassroomDetailVO;
import com.example.system.domain.classroom.vo.ClassroomSelectVO;
import com.example.system.domain.classroom.vo.ClassroomVO;
import com.example.system.mapper.classroom.ClassroomMapper;
import com.example.system.service.IClassroomService;
import com.example.system.service.IGroupService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class ClassroomServiceImpl implements IClassroomService {
    private static final Logger logger = LoggerFactory.getLogger(ClassroomServiceImpl.class);

    @Autowired
    private ClassroomMapper classroomMapper;

    @Autowired
    private IGroupService groupService;
    

    @Override
    public List<ClassroomVO> list(ClassroomQueryDTO classroomQueryDTO) {
        PageHelper.startPage(classroomQueryDTO.getPageNum(), classroomQueryDTO.getPageSize());
        List<ClassroomVO> classroomVOList = classroomMapper.selectClassroomList(classroomQueryDTO);
        return classroomVOList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(ClassroomSaveDTO classroomAddDTO) {
        List<Classroom> classroomList = classroomMapper.selectList(new LambdaQueryWrapper<Classroom>()
                .eq(Classroom::getClassroomName, classroomAddDTO.getClassroomName()));
        if(CollectionUtil.isNotEmpty(classroomList)){
            // 班级不能重名
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Classroom classroom = new Classroom();
        classroom.setClassroomName(classroomAddDTO.getClassroomName());
        classroom.setClassroomType(classroomAddDTO.getClassroomType());
        classroom.setStartDate(classroomAddDTO.getStartDate());
        classroom.setBio(classroomAddDTO.getBio());
        classroom.setStatus(classroomAddDTO.getStatus());

        // 先保存班级获取班级ID
        int result = classroomMapper.insert(classroom);
        
        // 创建群组
        Group group = new Group();
        group.setGroupId(classroom.getClassroomId()); // 将班级ID作为群组号
        group.setName(classroom.getClassroomName());  // 群名设置为班级名称
        groupService.addGroup(group);

        GroupMember groupMember = new GroupMember();
        groupMember.setGroupId(classroom.getClassroomId());
        groupMember.setUserId(ThreadLocalUtil.get(Constants.USER_ID,Long.class));
        groupMember.setNickname(ThreadLocalUtil.get(Constants.USER_NAME,String.class));
        groupMember.setIdentity(ThreadLocalUtil.get(Constants.USER_IDENTITY,Integer.class));
        groupService.addGroupMember(groupMember);
        return result;
    }

    @Override
    public ClassroomDetailVO detail(Long classroomId) {
        Classroom classroom = classroomMapper.selectById(classroomId);
        if(classroom == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        ClassroomDetailVO classroomDetailVO = new ClassroomDetailVO();
        BeanUtil.copyProperties(classroom, classroomDetailVO);
        return classroomDetailVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(ClassroomSaveDTO classroomSaveDTO) {
        Classroom oldClassroom = classroomMapper.selectById(classroomSaveDTO.getClassroomId());
        if(oldClassroom == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        oldClassroom.setClassroomName(classroomSaveDTO.getClassroomName());
        oldClassroom.setClassroomType(classroomSaveDTO.getClassroomType());
        oldClassroom.setStartDate(classroomSaveDTO.getStartDate());
        oldClassroom.setStatus(classroomSaveDTO.getStatus());
        oldClassroom.setBio(classroomSaveDTO.getBio());
        return classroomMapper.updateById(oldClassroom);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long classroomId) {
        Classroom oldClassroom = classroomMapper.selectById(classroomId);
        if(oldClassroom == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        groupService.delGroup(classroomId);
        return classroomMapper.deleteById(classroomId);
    }

    @Override
    public List<ClassroomSelectVO> selectList() {
        List<Classroom> classroomList = classroomMapper.selectList(new LambdaQueryWrapper<>());
        return BeanUtil.copyToList(classroomList, ClassroomSelectVO.class);
    }
}
